//
//  NSString+Tools.m
//  CarService
//
//  Created by Nimo.Yu on 15/1/30.
//  Copyright (c) 2015年 lexingsoft. All rights reserved.
//

#import "NSString+Tools.h"

#import <CommonCrypto/CommonDigest.h>

@implementation NSString (Tools)

+ (NSString *) md5:(NSString *) input
{
    if ([NSString checkNullString:input]) {
        return nil;
    }
    const char *cStr = [input UTF8String];
    unsigned char digest[16];
    CC_MD5( cStr, (CC_LONG)strlen(cStr), digest ); // This is the md5 call
    
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", digest[i]];
    
    return  output;
    
}

+ (NSString *) sha1:(NSString *)input{
    if ([NSString checkNullString:input]) {
        return nil;
    }
    const char *cStr = [input UTF8String];
    unsigned char digest[CC_SHA1_DIGEST_LENGTH];
    CC_SHA1( cStr, (CC_LONG)strlen(cStr), digest ); // This is the sha1 call
    
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
    
    for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", digest[i]];
    
    return  output;
}

///计算文本宽度
+ (CGFloat)calcWidthText:(NSString *)text font:(UIFont *)font
{
    NSDictionary *attributes = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
    CGSize textSize = [text sizeWithAttributes:attributes];
    return textSize.width;
}

///计算文本Size
+ (CGSize)calcSizeText:(NSString *)text font:(UIFont *)font
{
    NSDictionary *attributes = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
    CGSize textSize = [text sizeWithAttributes:attributes];
    return textSize;
}

///有效小数
+ (NSString *) stringFromDecimalNumber:(NSNumber *)number
{
    if (![number isKindOfClass:[NSNumber class]]) {
        return [number description];
    }
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    formatter.numberStyle = NSNumberFormatterDecimalStyle;
    return [formatter stringFromNumber:number];
}

@end
